home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
cool
/
ge_cool.lha
/
GE_COOL2.1
/
src
/
Vector
/
Base_Vector.C
next >
Wrap
C/C++ Source or Header
|
1992-04-23
|
11KB
|
305 lines
//
// Copyright (C) 1991 Texas Instruments Incorporated.
//
// Permission is granted to any individual or institution to use, copy, modify,
// and distribute this software, provided that this complete copyright and
// permission notice is maintained, intact, in all copies and supporting
// documentation.
//
// Texas Instruments Incorporated provides this software "as is" without
// express or implied warranty.
//
// Created: MBN 08/31/89 -- Initial design and implementation
// Updated: VDN 02/21/92 -- new lite version
//
// The CoolVector class is no longer derived from the Generic class and is used to
// implement non-type specific functionality for the parameterized CoolVector<Type>
// class. In this manner, code common to all instances of the CoolVector class can
// be shared to reduce code replication. The CoolVector<Type> class is dynamic in
// the sense that an object can grow dynamically if necessary. The growth size
// is determined by the value of a static allocation size variable for the
// class. However, fixed length vectors are also supported by setting this
// variable to INVALID. The CoolVector class implements the notion of a current
// position. This is useful for iterating through the elements of a vector.
// The current position is maintained in an integer and is set or reset by all
// methods affecting elements in the CoolVector class. Methods to reset, move to
// the next and previous, find, and get the value at the current position are
// provided.
//
#ifndef BASE_VECTORH // If no CoolVector class
#include <cool/Base_Vector.h> // Include the class header
#endif
#ifdef __cplusplus
float CoolVector::growth_ratio = 0.0; // Initialize growth ratio
#endif
// CoolVector () -- Empty constructor for the CoolVector class.
// Input: None
// Output: None
CoolVector::CoolVector () {
this->size = 0; // No storage allocated
this->number_elements = 0; // No elements in CoolVector
this->curpos = INVALID; // Invalidate current position
alloc_size = MEM_BLK_SZ; // Set the default size
#ifndef __cplusplus
if (this->growth_ratio == 0.0) // If not initialized
this->growth_ratio = 0.0; // Initialize growth ratio
#endif
}
// CoolVector (long) -- Constructor that specifies number of elements.
// Input: Number of elements
// Output: None
CoolVector::CoolVector (long n) {
this->size = n; // Element capacity
this->number_elements = 0; // No elements in Vector
this->curpos = INVALID; // Invalidate current position
alloc_size = MEM_BLK_SZ; // Set memory block size
#ifndef __cplusplus
if (this->growth_ratio == 0.0) // If not initialized
this->growth_ratio = 0.0; // Initialize growth ratio
#endif
}
// CoolVector (CoolVector&) -- Constructor for reference to another CoolVector
// Input: CoolVector reference
// Output: None
CoolVector::CoolVector (const CoolVector& v) {
#ifndef __cplusplus
if (this->growth_ratio == 0.0) // If not initialized
this->growth_ratio = 0.0; // Initialize growth ratio
#endif
alloc_size = v.alloc_size; // Set the default size
if (alloc_size == INVALID) // If invalid allocation size
alloc_size = MEM_BLK_SZ; // Set default size
this->size = v.size; // Copy element capacity
this->number_elements = v.number_elements; // Same number of elements
this->curpos = INVALID; // Invalidate current position
}
// ~CoolVector () -- Destructor for CoolVector class that frees up storage.
// Input: None
// Output: None
CoolVector::~CoolVector() {
}
// CoolVector& operator= () -- Overload the assignment operator
// Input: Reference to CoolVector object
// Output: Reference to copied Vector object
CoolVector& CoolVector::operator= (const CoolVector& v) {
this->number_elements = v.number_elements; // Copy number of elements
this->growth_ratio = v.growth_ratio; // Copy the growth ratio
this->curpos = INVALID; // Invalidate current position
return *this; // Return CoolVector reference
}
// clear() -- Removes all elements from this CoolVector
// (included for compatability with List)
// Input: None.
// Output: None.
void CoolVector::clear() {
this->reset(); // make current position invalid
this->number_elements = 0;
}
// is_empty() -- Indicates empty CoolVector
// (included for compatability with List)
// Input: None.
// Output: TRUE or FALSE.
Boolean CoolVector::is_empty() {
return this->number_elements == 0;
}
// long set_length () -- Set the number of elements in a CoolVector object.
// If there is not enough storage allocated, set to
// maximum size for storage.
// Input: Length, type
// Output: Integer representing number of elements
long CoolVector::set_length (long n, const char* Type) {
#if ERROR_CHECKING
if (n < 0) { // If index out of range
//RAISE (Error, SYM(CoolVector), SYM(Negative_Length),
printf ("CoolVector<%s>::set_length(): Negative length %d.\n", Type, n);
abort ();
}
#endif
if (n <= size) // If not greater than size
this->number_elements = n; // Set new length
else
this->number_elements = size; // Else set to maximum possible
return this->number_elements; // Return value
}
// void set_growth_ratio (float) -- Set the growth percentage for the CoolVector
// object.
// Input: Float ratio, type
// Output: None
void CoolVector::set_growth_ratio (float ratio, const char* Type) {
#if ERROR_CHECKING
if (ratio <= 0.0) { // If non-positive growth
//RAISE (Error, SYM(CoolVector), SYM(Negative_Ratio),
printf ("CoolVector<%s>::set_growth_ratio(): Negative growth ratio %f.\n",
Type, ratio);
abort ();
}
#endif
this->growth_ratio = ratio; // Adjust ration
}
// void set_alloc_size (int) -- Set the default allocation size growth rate.
// Input: Growth size in number of elements, type
// Output: None
void CoolVector::set_alloc_size (int n, const char* Type) {
#if ERROR_CHECKING
if (n < 0) { // If index out of range
//RAISE (Error, SYM(CoolVector), SYM(Negative_Size),
printf ("CoolVector<%s>::set_alloc_size(): Negative growth size %d.\n",
Type, n);
abort ();
}
#endif
this->alloc_size = n; // Set growth size
}
//----------------------------no error exception----------------------------
// bracket_error -- Raise exception for CoolVector<Type>::operator[]()
// Input: Type string
// Output: None
void CoolVector::bracket_error (const char* Type, const long n) const {
//RAISE (Error, SYM(CoolVector), SYM(Out_Of_Range),
printf ("CoolVector<%s>::operator[](): Index %d out of range.\n", Type, n);
abort ();
}
// value_error -- Raise exception for CoolVector<Type>::value()
// Input: Type string
// Output: None
void CoolVector::value_error (const char* Type) const {
//RAISE (Error, SYM(CoolVector), SYM(Invalid_Cpos),
printf ("CoolVector<%s>::value(): Invalid current position.\n", Type);
abort ();
}
// resize_error -- Raise exception for CoolVector<Type>::resize()
// Input: Type string, size argument
// Output: None
void CoolVector::resize_error (const char* Type, const long n) const {
//RAISE (Error, SYM(CoolVector), SYM(Negative_Size),
printf ("CoolVector<%s>::resize(): Negative resize %d.\n", Type, n);
abort ();
}
// static_error -- Raise exception for CoolVector<Type>::resize()
// Input: Type string, size argument
// Output: None
void CoolVector::static_error (const char* Type) const {
//RAISE (Error, SYM(CoolVector), SYM(Static_Size),
printf ("CoolVector<%s>::resize(): Static-size CoolVector.\n", Type);
abort ();
}
// assign_error -- Raise exception for CoolVector<Type>::operator=()
// Input: Type string
// Output: None
void CoolVector::assign_error (const char* Type) const {
//RAISE (Error, SYM(CoolVector), SYM(Static_Size),
printf ("CoolVector<%s>::operator=(): Static-size CoolVector.\n", Type);
abort ();
}
// fill_start_error -- Raise exception for CoolVector<Type>::fill()
// Input: Type string, start argument
// Output: None
void CoolVector::fill_start_error (const char* Type, const long start) const {
//RAISE (Error, SYM(CoolVector), SYM(Out_Of_Range),
printf ("CoolVector<%s>::fill(): Start index %d out of range.\n", Type, start);
abort ();
}
// fill_end_error -- Raise exception for CoolVector<Type>::fill()
// Input: Type string, end argument
// Output: None
void CoolVector::fill_end_error (const char* Type, const long end) const {
//RAISE (Error, SYM(CoolVector), SYM(Out_Of_Range),
printf ("CoolVector<%s>::fill(): End index %d out of range.\n", Type, end);
abort ();
}
// copy_start_error -- Raise exception for CoolVector<Type>::copy()
// Input: Type string, start argument
// Output: None
void CoolVector::copy_start_error (const char* Type, const long start) const {
//RAISE (Error, SYM(CoolVector), SYM(Out_Of_Range),
printf ("CoolVector<%s>::copy(): Start index %d out of range.\n", Type, start);
abort ();
}
// copy_end_error -- Raise exception for CoolVector<Type>::copy()
// Input: Type string, end argument
// Output: None
void CoolVector::copy_end_error (const char* Type, const long end) const {
//RAISE (Error, SYM(CoolVector), SYM(Out_Of_Range),
printf ("CoolVector<%s>::copy(): End index %d out of range.\n", Type, end);
abort ();
}
// copy_error -- Raise exception for CoolVector<Type>::copy()
// Input: Type string
// Output: None
void CoolVector::copy_error (const char* Type) {
//RAISE (Error, SYM(CoolVector), SYM(Static_Size),
printf ("CoolVector<%s>::copy(): Static-size CoolVector.\n", Type);
abort ();
}
// remove_error -- Raise exception for CoolVector<Type>::remove()
// Input: Type string
// Output: None
void CoolVector::remove_error (const char* Type) const {
//RAISE (Error, SYM(CoolVector), SYM(Invalid_Cpos),
printf ("CoolVector<%s>::remove(): Invalid current position.\n", Type);
abort ();
}
// va_arg_error -- Raise exception for using class objects, or chars in (...)
// Input: Type string
// Output: None
void CoolVector::va_arg_error (const char* Type, int n) {
//RAISE (Error, SYM(CoolVector), SYM(Invalid_Va_Arg),
printf ("CoolVector<%s>::CoolVector<%s>(): Invalid type in ... or wrong alignment with %d bytes.\n",
Type, Type, n);
abort ();
}